package com.hxxy.wyp.petsLover.common.rest.base.fronted;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.req.*;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.AuthInfo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.CenterInfoVo;
import com.hxxy.wyp.petsLover.common.services.base.fronted.UserService;
import com.hxxy.wyp.petsLover.security.SecurityContextFrontUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserFrontRestBase
 * @Description 前台用户相关接口
 * @Author wyp
 * @Date 2023/1/2 20:07
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/user")
public class UserFrontRestBase extends RestBase {

    @Autowired
    private UserService userService;


    /**
     * 显示所有用户列表
     *
     * @return
     */
    @RequestMapping("/list")
    public Object list() {
        List<User> list = userService.list();
        return ajaxSuccess(list, "200");
    }

    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "user_load"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Object page(@RequestBody SearchObj searchObj) {
        long page = Long.parseLong(searchObj.getPage());
        long pageSize = Long.parseLong(searchObj.getPageSize());
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getId()), User::getId, searchObj.getId());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getUserName()), User::getUserName, searchObj.getUserName());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getEmail()), User::getEmail, searchObj.getEmail());
        lambdaQueryWrapper.ge(searchObj.getBeginDate() != null, User::getRegistrationTime, searchObj.getBeginDate());
        lambdaQueryWrapper.le(searchObj.getEndDate() != null, User::getRegistrationTime, searchObj.getEndDate());
        lambdaQueryWrapper.orderByDesc(User::getRegistrationTime);
        userService.pageSearch(pageInfo, lambdaQueryWrapper);
        return ajaxSuccess(pageInfo, "200", "查询成功！");
    }

    /**
     * 根据id查询详细信息
     *
     * @param id
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "user_load"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping("/findById")
    public Object findById(@RequestParam("id") String id) {
        User user = userService.getById(id);
        assert user != null;
        return ajaxSuccess(user, "200");
    }

    /**
     * 根据id删除用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "user_delete"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object deleteById(@RequestParam("id") String id) {
        boolean isDeleted = userService.removeById(id);
        if (isDeleted) {
            return ajaxSuccess("200", "删除成功!");
        }
        return ajaxError("500", "删除失败!");
    }

    /**
     * 更新用户(后台管理员)
     *
     * @param user
     * @return
     */
    @RequestMapping("updateByUser")
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "user_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object updateByUser(@RequestBody User user) {
        int i = userService.updateByUser(user);
        if (i > 0) return ajaxSuccess("200", "更新成功!");
        return ajaxError("500", "更新失败!");
    }

    /**
     * 更新用户(前台用户)
     *
     * @param user
     * @return
     */
    @RequestMapping("updateByUserSelf")
    @SaCheckLogin
    public Object updateByUserSelf(@RequestBody User user) {
        int i = userService.updateByUser(user);
        if (i > 0) return ajaxSuccess("200", "更新成功!");
        return ajaxError("500", "更新失败!");
    }

    /**
     * 启用或禁用用户
     *
     * @param banIdReq
     * @return
     */
    @RequestMapping(value = "/enableOrDisableUser", method = RequestMethod.POST)
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "user_open_close"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object enableOrDisableUser(@RequestBody BanIdReq banIdReq) {
        Integer i = userService.enableOrDisableUser(Long.parseLong(banIdReq.getId()));
        if (i > 0) {
            return ajaxSuccess("200", "修改成功!");
        }
        return ajaxSuccess("500", "修改失败！");
    }

    /**
     * 修改密码
     *
     * @param ruleFormReq
     * @return
     */
    @RequestMapping(value = "/updatePass", method = RequestMethod.POST)
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "user_update_password"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object updatePass(@RequestBody RuleFormReq ruleFormReq) {

        Integer i = userService.updatePass(ruleFormReq);
        if (i > 0) {
            return ajaxSuccess("200", "修改密码成功!");
        }
        return ajaxError("修改密码失败!");
    }



    @RequestMapping(value = "/updatePassByUser", method = RequestMethod.POST)
    @SaCheckLogin
    public Object updatePassByUser(@RequestBody RuleFormReq ruleFormReq) {

        Integer i = userService.updatePass(ruleFormReq);
        if (i > 0) {
            return ajaxSuccess("200", "修改密码成功!");
        }
        return ajaxError("修改密码失败!");
    }

    /**
     * 前台用户登录
     *
     * @param userLoginInfo
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object userLogin(@RequestBody UserLoginInfo userLoginInfo) {
        AuthInfo authInfo = userService.login(userLoginInfo);
        return ajaxSuccess(authInfo, "200", "登录成功!");
    }

    @SaCheckLogin
    @RequestMapping("/logout")
    public Object userLogout() {
        userService.logout();
        return ajaxSuccess("200", "退出成功!");
    }

    /**
     * 前台用户注册
     */
    @RequestMapping("/register")
    public Object userRegister(@RequestBody UserRegisterReq userRegisterReq) {
        Integer i = userService.register(userRegisterReq);
        if (i > 0) {
            return ajaxSuccess("200", "注册成功!");
        }
        return ajaxError("500", "注册失败!");
    }

    /**
     * 获取验证码
     */
    @RequestMapping("/getVerificationCode")
    public Object getCode(@RequestBody ValidatedCodeReq validatedCodeReq) {
        userService.getCode(validatedCodeReq);
        return ajaxSuccess("200", "发送成功!");
    }


    /**
     * 获取个人中心所有信息
     *
     * @return
     */
    @RequestMapping("/centerInfo")
    @SaCheckLogin
    public Object centerInfo() {

        CenterInfoVo centerInfoVo = userService.getCenterInfo();
        return ajaxSuccess(centerInfoVo, "200", "查询成功!");
    }

    /**
     * 发送售后信息
     */
    @RequestMapping("/sendSaleBackMsg")
    public Object getCode(@RequestParam("orderId") String orderId) {
        userService.sendSaleBackMsg(orderId);
        return ajaxSuccess("200", "发送成功!");
    }


}
